
import { _decorator, Component, Node, SpriteFrame, Sprite } from 'cc';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = AnimPlayer
 * DateTime = Sat Feb 05 2022 15:06:43 GMT+0800 (China Standard Time)
 * Author = vvc_zeng
 * FileBasename = AnimPlayer.ts
 * FileBasenameNoExtension = AnimPlayer
 * URL = db://assets/script/utils/AnimPlayer.ts
 * ManualUrl = https://docs.cocos.com/creator/3.3/manual/zh/
 *
 */
 
@ccclass('AnimPlayer')
export class AnimPlayer extends Component {
    // 使用计时器来播放帧动画
    index: number;
    timer: number;

    @property
    speed: number = 0.1;
    @property
    sprites: SpriteFrame[] = [];
    @property
    isPlay: boolean = false;
    update(dt: number) {
        if (this.isPlay) {
            this.timer += dt;
            if (this.timer > this.speed) {
                this.timer = 0;
                //  切换帧
                this.index++;
                if (this.index >= this.sprites.length) {
                    this.index = 0;
                }
                (<Sprite>this.getComponent("cc.Sprite")).spriteFrame = this.sprites[this.index];
            }
        }
    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.3/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.3/manual/zh/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.3/manual/zh/scripting/life-cycle-callbacks.html
 */
